<?php
/**
 * Created by PhpStorm.
 * User: lx
 * Date: 2020/1/2
 * Time: 10:23
 */

namespace app\api\controller;

use app\admin\controller\Express;
use data\service;
use data\service\Order as OrderService;

class Order
{
    /**
     * 个人订单列表
     *
     * @return string
     * @throws \Exception
     * @author dzl
     */
    public function orderList()
    {
        $page_index = request()->post('pageIndex', 1);
        $page_size = request()->post('page_size', PAGESIZE);
        $start_date = request()->post('start_date') == "" ? 0 : getTimeTurnTimeStamp(request()->post('start_date'));
        $end_date = request()->post('end_date') == "" ? 0 : getTimeTurnTimeStamp(request()->post('end_date'));
        $user_name = request()->post('user_name', '');
        $order_no = request()->post('order_no', '');
        $order_status = request()->post('order_status', '');
        $receiver_mobile = request()->post('receiver_mobile', '');
        $payment_type = request()->post('payment_type', '');
        $shop_id = request()->post('shop_id', -1);
        $id = request()->post('id', 0);

        if ($start_date != 0 && $end_date != 0) {
            $condition["create_time"] = [
                [
                    ">",
                    $start_date
                ],
                [
                    "<",
                    $end_date
                ]
            ];
        } elseif ($start_date != 0 && $end_date == 0) {
            $condition["create_time"] = [
                [
                    ">",
                    $start_date
                ]
            ];
        } elseif ($start_date == 0 && $end_date != 0) {
            $condition["create_time"] = [
                [
                    "<",
                    $end_date
                ]
            ];
        }
        if ($order_status != '') {
            // $order_status 1 待发货
            if ($order_status == 1) {
                // 订单状态为待发货实际为已经支付未完成还未发货的订单
                $condition['shipping_status'] = 0; // 0 待发货
                $condition['pay_status'] = 2; // 2 已支付
                $condition['order_status'] = array('neq', 4); // 4 已完成
                $condition['order_status'] = array('neq', 5); // 5 关闭订单
            } else
                $condition['order_status'] = $order_status;
        }
        if (!empty($payment_type)) {
            $condition['payment_type'] = $payment_type;
        }
        if (!empty($user_name)) {
            $condition['receiver_name'] = $user_name;
        }
        if (!empty($order_no)) {
            $condition['order_no'] = array(
                "like",
                "%" . $order_no . "%"
            );
        }
        if (!empty($receiver_mobile)) {
            $condition['receiver_mobile'] = $receiver_mobile;
        }
        if ($shop_id > -1) {
            $condition['shop_id'] = $shop_id;
        }
        $condition['buyer_id'] = $id;
        $order_service = new OrderService();
        $list = $order_service->getMyOrderList($page_index, $page_size, $condition, 'create_time desc');
        return return_json_data($list);
    }

    /**
     * 订单详情
     *
     * @return string
     * @throws \Exception
     * @author dzl
     */
    public function orderInfo(){
        $order_id = isset($_GET['order_id']) ? $_GET['order_id'] : 23;

        if ($order_id == 0) {
            return return_json_data("没有获取到订单信息");
        }
        $order_service = new OrderService();
        $detail = $order_service->getOrderDetail($order_id);

        if (empty($detail)) {
            return return_json_data("没有获取到订单信息");
        }

          return return_json_data($detail);

    }

    /**
     * 申请退款
     *
     * @return string
     * @throws \Exception
     * @author dzl
     */
    public function orderGoodsRefundAskfor()
    {
        $order_id = request()->post('order_id', 0);
        $order_goods_id = request()->post('order_goods_id', 0);
        $refund_type = request()->post('refund_type', 1);
        $refund_require_money = request()->post('refund_require_money', 0);
        $refund_reason = request()->post('refund_reason', '');
        $order_service = new OrderService();
        $retval = $order_service->orderGoodsRefundAskfor($order_id, $order_goods_id, $refund_type, $refund_require_money, $refund_reason);
        return return_json_data($retval);
    }

    /**
     * 申请退款
     *
     * @return string
     * @throws \Exception
     */
    public function refundDetail()
    {
        $order_goods_id = request()->get('order_goods_id', 0);
        $id = request()->get('id', 0);
        $instance_id = request()->get('instance_id', 0);

        if (! is_numeric($order_goods_id) || $order_goods_id == 0) {
            return return_json_data("没有获取到退款信息",500);
        }
        $order_service = new OrderService();
        $detail = $order_service->getOrderGoodsRefundInfo($order_goods_id);
        $data['detail'] = $detail;

        $condition['order_goods_id'] = $order_goods_id;
        $condition['buyer_id'] =$id;
        $count = $order_service->getUserOrderGoodsCountByCondition($condition);
        if ($count == 0) {
            return return_json_data("对不起,您无权进行此操作",500);
        }

        // 实际可退款金额
        $refund_money = $order_service->orderGoodsRefundMoney($order_goods_id);
        $data['refund_money'] = sprintf("%.2f", $refund_money);

        // 余额退款
        $order_goods_service = new OrderService\OrderGoods();
        $refund_balance = $order_goods_service->orderGoodsRefundBalance($order_goods_id);
        $data['refund_balance'] = sprintf("%.2f", $refund_balance);

        // 查询店铺默认物流地址
        $express = new service\Express();
        $address = $express->getDefaultShopExpressAddress($instance_id);
        $data['address_info'] = $address;

        // 查询商家地址
        $member = new service\Member();
        $shop_info = $order_service->getShopReturnSet($instance_id);
        $data['shop_info'] = $shop_info;
        $member_detail = $member->getMemberDetail($instance_id);
        $data['member_detail'] = $member_detail;

        return return_json_data($data);

    }

}